//
//  SearchViewController.m
//  网络FM
//
//  Created by dengzhicheng on 15/5/21.
//  Copyright (c) 2015年 dengzhicheng.com. All rights reserved.
//
#import "ShowImageCell.h"
//#import "CircleLayout.h"
#import "SearchViewController.h"
#import "SearchSecondCollectionViewCell.h"
#import "ChoiceButton.h"
#import "AnchorBurron.h"
#import "AnchorViewController.h"
#import "FeelingViewController.h"
#import "SearchTitleViewController.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "Cover.h"
#import "Anchor.h"
#import "GFPlaceholderView.h"

//真正的搜索界面
#import "SearchByChooseViewController.h"
#import "SBPageFlowView.h"
///#import "MyImageView.h"
#import "SearchBarView.h"


@interface SearchViewController ()<SBPageFlowViewDelegate,SBPageFlowViewDataSource>
{
    NSInteger    _currentPage;
    SBPageFlowView *_flowView;
}

@property (nonatomic,retain)NSArray *choiceArray;
@property (nonatomic, retain)GFPlaceholderView *placeholder;


@end

@implementation SearchViewController


- (void)dealloc
{
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.coverArray = [NSMutableArray array];
        self.anchorArray = [NSMutableArray array];
        self.automaticallyAdjustsScrollViewInsets = NO;
        
        self.choiceArray = [[NSMutableArray alloc]initWithObjects:@"烦躁",@"悲伤", @"孤独", @"已弃疗",@"减压",@"无奈",@"快乐",@"感动",@"迷茫",@"睡前",@"旅行",@"散步",@"坐车",@"独处",@"失恋",@"失眠",@"随便", @"无聊",nil];
        
        
    }
    return self;
}

#pragma mark -
-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self createSearchbutton];
}


- (void)loadView
{
    [super loadView];
}

- (void)viewDidLoad {
    [super loadView];
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, - 64, self.view.frame.size.width, self.view.frame.size.height)];
    [view setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:view];
    [view release];

    [self createScrollView];
    [self createGroupLable];
    [self createMybutton];
    [self SearchConnetion];
    self.navigationController.navigationBar.translucent = NO;
}


#pragma mark - 创建最上面的title的滚动视图
- (void)creatSearchTitleScroll
{
    _flowView = [[SBPageFlowView alloc] initWithFrame:CGRectMake(0, 1, self.view.bounds.size.width, 138)];
    _flowView.delegate = self;
    _flowView.dataSource = self;
    
    //非主要显示的图片淡化显示
    _flowView.minimumPageAlpha = 0.4;
    
    _flowView.minimumPageScale = 1;
    [self.view addSubview:_flowView];
    [_flowView reloadData];
    [_flowView release];
  
}


//pragma mark - PagedFlowView Datasource
//返回显示View的个数
- (NSInteger)numberOfPagesInFlowView:(SBPageFlowView *)flowView{
    return 5;
}

- (CGSize)sizeForPageInFlowView:(SBPageFlowView *)flowView;{
    return CGSizeMake(240, 140);
}

//返回给某列使用的View
- (UIView *)flowView:(SBPageFlowView *)flowView cellForPageAtIndex:(NSInteger)index{
    
    UIImageView *imageView = (UIImageView *)[flowView dequeueReusableCell];
    if (!imageView) {
        imageView = [[[UIImageView alloc] init] autorelease];
        imageView.layer.masksToBounds = YES;
    }
    Cover *model = [self.coverArray objectAtIndex:index];
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:nil options:SDWebImageRetryFailed];
    
    return imageView;
}


#pragma mark - PagedFlowView Delegate

//为后续滚动添加图片
- (void)didReloadData:(UIView *)cell cellForPageAtIndex:(NSInteger)index
{
    Cover *model = [self.coverArray objectAtIndex:index];
    UIImageView *imageView = (UIImageView *)cell;
    [imageView sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:nil options:SDWebImageRetryFailed];
}


//点击跳转
- (void)didSelectItemAtIndex:(NSInteger)index inFlowView:(SBPageFlowView *)flowView
{
    
    Cover *model = [self.coverArray objectAtIndex:index];
    SearchTitleViewController *title = [[SearchTitleViewController alloc]init];
    title.model = model;
    [self.navigationController pushViewController:title animated:YES];
}





- (void)SearchConnetion
{
    
    //滚动视图的网络请求
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *str = [NSString stringWithFormat:@"http://bapi.xinli001.com/fm2/hot_tag_list.json/?flag=4"] ;
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array = [bigDic objectForKey:@"data"];
        for (NSMutableDictionary *dic  in array) {
            Cover *model = [[Cover alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.coverArray addObject:model];
            
        }
        [self creatSearchTitleScroll];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    
    //主播网络请求
    AFHTTPRequestOperationManager *manager1 = [AFHTTPRequestOperationManager manager];
    NSString *str1 = [NSString stringWithFormat:@"http://bapi.xinli001.com/fm2/speaker_list.json/?v=1&key=c605710a2343f6da4ac22c8cce194bd0&rows=6&offset=0"];
    [manager1 GET:str1 parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *bigDic1 = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array1 = [bigDic1 objectForKey:@"data"];
        for (NSMutableDictionary *dic1  in array1) {
            Anchor *anchorModel = [[Anchor alloc]init];
            [anchorModel setValuesForKeysWithDictionary:dic1];
            [self.anchorArray addObject:anchorModel];
        }
        [self createAnchor];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}



#pragma mark - 创建搜索按钮
- (void)createSearchbutton
{
    float width = self.view.frame.size.width;
    UIView *myview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width - 40, 32)];
    [myview setCenter:CGPointMake(width / 2, 160)];
    [myview setBackgroundColor:[UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1.0f]];
    [myview.layer setCornerRadius:6];
    [self.view addSubview:myview];
    [myview release];
    
    UIButton *search = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, width - 70 , 28)];
    [search setCenter: CGPointMake(myview.frame.size.width / 2, 16)];
    [search setTitle:@"请输入主播名/场景" forState:UIControlStateNormal];
    [search.titleLabel setFont:[UIFont systemFontOfSize:14]];
    
    [search setBackgroundColor:[UIColor whiteColor]];
    [search setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [search addTarget:self action:@selector(searchForward:) forControlEvents:UIControlEventTouchUpInside];
    [myview addSubview:search];
    [search release];
    
    UIImageView *ima = [[ UIImageView alloc]initWithFrame:CGRectMake(5, 5, 20, 20)];
    [search addSubview:ima];
    UIImage *i = [UIImage imageNamed:@"font-search.png"];
    [ima setImage:i ];
    [ima release];
    
}

//搜索按钮的点击事件
- (void)searchForward:(id)sender
{
    SearchByChooseViewController *s = [[SearchByChooseViewController alloc]init];
    [self.navigationController pushViewController:s  animated:YES];
    
}


#pragma mark - 创建scrollView (用于呈现心情,主播,场景等按钮)
- (void)createScrollView
{
    float width = [UIScreen mainScreen].bounds.size.width;
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 180, self.view.frame.size.width,self.view.frame.size.height - self.view.frame.size.width /  3 - 60)];
    [self.scroll setContentSize:CGSizeMake(0,2.75 * width + 240)];
    [self.scroll setBackgroundColor:[UIColor whiteColor]];
    self.scroll.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.scroll];
    
}


#pragma mark - 创建各种lable
- (void)createGroupLable
{
    float width = [UIScreen mainScreen].bounds.size.width;
    
    UILabel  *feeling = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, width - 20, 30)];
    [feeling setText:@"心情"];
    feeling.font = [UIFont boldSystemFontOfSize:22];
    [self.scroll addSubview:feeling];
    [feeling release];
    
    UILabel *scene = [[UILabel alloc]initWithFrame:CGRectMake(20, width + 60, width - 20, 30)];
    [scene setText:@"场景"];
    scene.font = [UIFont boldSystemFontOfSize:22];
    [self.scroll addSubview:scene];
    [scene release];
    
    UILabel *anchor = [[UILabel alloc]initWithFrame:CGRectMake(20, width * 2 + 120, width - 20, 30)];
    [anchor setText: @"主播"];
    anchor.font = [UIFont boldSystemFontOfSize:22];
    [self.scroll addSubview:anchor];
    [anchor release];
    
}



#pragma mark- 创建各种按钮
- (void)createMybutton
{
    for (int i = 0;  i < 18; i ++) {
        if (i < 9) {
            
            ChoiceButton * b = [[ChoiceButton alloc]initWithFrame:CGRectMake(i % 3 * (self.view.frame.size.width / 3), i / 3 * (self.view.frame.size.width / 3) + 50, (self.view.frame.size.width / 3), (self.view.frame.size.width / 3))];
            int a = i + 1;
            b.layer.borderWidth = 4;
            CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
            CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0.1, 0.1, 0.1, 0.1 });
            [b.layer setBorderColor:colorref];
            b.layer.cornerRadius = 8;
            NSString *name = [NSString stringWithFormat:@"%d.png",a];
            [b.view setImage:[UIImage imageNamed:name]];
            
            [self.scroll addSubview:b];
            b.desc.text  = [self.choiceArray objectAtIndex:i];
            [b.desc  setFont:[UIFont systemFontOfSize:14]];
            [b.desc setTextAlignment:NSTextAlignmentCenter];
            [b.desc setTextColor:[UIColor blackColor]];
            b.tag = i + 1;
            b.desc  = [self.choiceArray objectAtIndex:i];
            [b  addTarget:self action:@selector(buttonAction: ) forControlEvents:UIControlEventTouchUpInside];
            [b release];
        }
        else
        {
            ChoiceButton * b = [[ChoiceButton alloc]initWithFrame:CGRectMake(i % 3 * (self.view.frame.size.width / 3), i / 3 * (self.view.frame.size.width / 3 ) + 100 , (self.view.frame.size.width / 3), (self.view.frame.size.width / 3))];
            int a = i + 1;
            b.layer.borderWidth = 4;
            CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
            CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0.1, 0.1, 0.1, 0.1 });
            [b.layer setBorderColor:colorref];
            b.layer.cornerRadius = 8;
            
            
            NSString *name = [NSString stringWithFormat:@"%d.png",a];
            [b.view setImage:[UIImage imageNamed:name]];
            
            [self.scroll addSubview:b];
            b.tag = i + 1;
            b.desc.text  = [self.choiceArray objectAtIndex:i];
            [b.desc  setFont:[UIFont systemFontOfSize:14]];
            [b.desc setTextColor:[UIColor blackColor]];
            [b.desc setTextAlignment:NSTextAlignmentCenter];
            
            [b  addTarget:self action:@selector(buttonAction: ) forControlEvents:UIControlEventTouchUpInside];
            [b release];
        }
    }
    
}

//按钮的点击事件
- (void)buttonAction:(id)sender
{
    UIButton *button = (UIButton *)sender;
    FeelingViewController *fell = [[FeelingViewController alloc]init];
    fell.choice = [self.choiceArray objectAtIndex:(button.tag - 1)];
    [self.navigationController  pushViewController:fell animated:YES];
  
}


#pragma mark - 创建主播按钮
- (void)createAnchor
{
    float width = [UIScreen mainScreen].bounds.size.width;
    
    for (int i = 0;  i < 6;  i++) {
        AnchorBurron *anchor = [[AnchorBurron alloc]initWithFrame:CGRectMake(i % 2 * (width / 2), i / 2  * (width / 4) + width * 2 + 150, width / 2 , width / 4)];
        [anchor addTarget:self action:@selector(AnchorAction:) forControlEvents:UIControlEventTouchUpInside];
        anchor.tag = i + 1;
        Anchor *anchorModel = [self.anchorArray objectAtIndex:i ];
        NSURL *url = [NSURL URLWithString:anchorModel.avatar];
        
        anchor.view.userInteractionEnabled = NO;
        anchor.view.clipsToBounds = YES;
        anchor.view.layer.cornerRadius = 30;
        [anchor.view sd_setImageWithURL:url placeholderImage:nil options:SDWebImageRetryFailed];
        anchor.Num.text = [NSString stringWithFormat:@"%ld",(long)anchorModel.fmnum];
        anchor.Num.font = [UIFont systemFontOfSize:15];
        [anchor.Num  setTextAlignment:NSTextAlignmentCenter];
        
        anchor.anchorName.text = anchorModel.username;
        anchor.anchorName.font = [UIFont systemFontOfSize:15];
        [self.scroll addSubview:anchor];
        [anchor release];
    }
    
}

//主播按钮的点击事件
- (void)AnchorAction:(id)sender
{
    UIButton *button = (UIButton *)sender;
    AnchorViewController *fell = [[AnchorViewController alloc]init];
    
    //通过tag值把主播数组里的model拿出来
    Anchor *model = [self.anchorArray objectAtIndex:button.tag - 1];
    //跳转页面时将所点击的主播的model传过去
    fell.model = model;
    [self.navigationController  pushViewController:fell animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
